package org.blame.workQueue;

import com.rabbitmq.client.*;
import org.blame.constants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Customer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
//        1.建立连接
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL);

        Connection connection= connectionFactory.newConnection();

//        2.开启信道
        Channel channel=connection.createChannel();

//        消费消息
        DefaultConsumer consumer=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(" 接收到消息: " + new String(body));
            }
        };

        channel.basicConsume(Constants.QUEUE_NAME,true,consumer);
//        channel.close();
//        connection.close();
    }
}
